﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing.Imaging;
using System.IO;
using System.Xml;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.Web;
using System.Net;
using System.Collections.Specialized;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography;


using System.Collections;

namespace Business
{
    public partial class TimEsynchronous : Form
    {
        public MainForm m_mainform;
        [StructLayout(LayoutKind.Sequential)]
        public struct SystemTime
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMiliseconds;
        }
        // 用于设置系统时间  
        [DllImport("Kernel32.dll")]
        public static extern bool SetLocalTime(ref SystemTime sysTime);
        // 用于获得系统时间  
        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(ref SystemTime sysTime);

       

        public TimEsynchronous()
        {
            InitializeComponent();
           
        }


        public string decode(string str)//utf8转中文
        {
            Encoding utf8 = Encoding.UTF8;
            // 首先用utf-8进行解码                    
            return HttpUtility.UrlDecode(str.ToUpper(), utf8);
        }
        private void but_save_Click(object sender, EventArgs e)
        {
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("时间同步"))
            {
                m_mainform.baocunhxrj("时间同步", string.Format("时间同步"), "查询");// 核心功能日记  日记内容
            }

            string vehispara18C50 = "";
            string stoubao18C50 = "<?xml version=\"1.0\" encoding=\"GBK\"?>";
            vehispara18C50 = stoubao18C50 + string.Format("<root><babh>{0}</babh></root>", m_mainform.sjyjgbh);
            WebReference.Service m_sshenhe18C50 = new WebReference.Service();
            m_sshenhe18C50.Url = m_mainform.surl;


            string str = (m_sshenhe18C50.queryObjectOut("18", m_mainform.sSQM, "18C50", vehispara18C50));
            MessageBox.Show(decode(str));
            if (str.Length < 1)
            {
                MessageBox.Show("连接不到服务器");
            }
            else
            {
                XmlDocument xmlstr = new XmlDocument();
                xmlstr.LoadXml(str);
                if (xmlstr.SelectSingleNode("/root/head/code").InnerText == "1")
                {
                    string sdate = m_mainform.decode(xmlstr.SelectSingleNode("/root/body/vehispara/sj").InnerText);
                    text_times.Text = sdate;

                    //string str = "2016-10-10 10:10:20";
                    DateTime dt = DateTime.Parse(sdate);
                    // 创建SystemTime结构体，用于接收用户设置的时间   
                    SystemTime systemTime = new SystemTime();
                    // 从setDate,setTime控件中获取年，月，日，小时，分钟，秒信息，存入SystemTime结构体中       
                    systemTime.wYear = (ushort)dt.Year;
                    systemTime.wMonth = (ushort)dt.Month;
                    systemTime.wDay = (ushort)dt.Day;
                    systemTime.wHour = (ushort)dt.Hour;
                    systemTime.wMinute = (ushort)dt.Minute;
                    systemTime.wSecond = (ushort)dt.Second;
                    // 将系统的时间设置为用户指定的时间   
                    SetLocalTime(ref systemTime);
                }
            }
        }

        private void TimEsynchronous_Load(object sender, EventArgs e)
        {
            //判断是否民警
            if (m_mainform.sfjy.Contains("时间同步"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_save.Enabled = false;
                }
            }
        }
    }
}
